package com.kfm.mybatis.mapper;

import com.kfm.mybatis.model.Account;
import com.kfm.mybatis.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IAccountMapper {

    @Select("select id, uid, money from account")
    @Results(id = "result", value = {
        @Result(id = true, property = "id", column = "id"),
        @Result(property = "uid", column = "uid"),
        @Result(property = "money", column = "money"),
    })
    List<Account> selectAll();

    @Select("select * from account where uid = #{uid}")
    @ResultMap("result")
    List<Account> selectByUid(Integer uid);

    @Insert("insert into account(uid, money) values(#{uid}, #{money})")
    int insert(Account account);

    @Insert("insert into account(uid, money) values(#{uid}, #{money})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = Integer.class)
    int insertReturnKey(Account account);

    @Select("select a.*, u.id user_id, u.username, u.password  from account a, user u where a.uid = u.id and a.id = #{id}")
    @Results(id = "accountWithUser", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "uid", column = "uid"),
            @Result(property = "money", column = "money"),
            @Result(property = "user", javaType = User.class
                    ,one = @One(resultMap = "com.kfm.mybatis.mapper.IUserMapper.result"))
    })
    Account selectByIdWithUser(Integer id);


    @Select("select * from account a where id = #{id}")
    @Results(id = "accountWithUser2", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "uid", column = "uid"),
            @Result(property = "money", column = "money"),
            @Result(property = "user", column = "uid"
                    ,one = @One(select = "com.kfm.mybatis.mapper.IUserMapper.selectById"))
    })
    Account selectByIdWithUser2(Integer id);
}
